import axios, {AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig} from "axios";
import {Result} from "@/types/modules/data.ts";

const config = {
    baseURL: "http://192.168.8.24:3001",
    timeout: 200,
    withCredentials: true,
};

class HttpAxios {
    service: AxiosInstance;

    constructor(config: AxiosRequestConfig) {
        this.service = axios.create(config);
        this.service.interceptors.request.use(
            (config: AxiosRequestConfig) => {
                let language = "tw";
                config.headers ['Accept-Language'] = language
                return {
                    ...config,
                } as InternalAxiosRequestConfig<any>;
            },
            (error: AxiosError) => {
                return Promise.reject(error);
            },
        );
        this.service.interceptors.response.use(
            (resp: AxiosResponse<any>) => {
                const {data} = resp;
                return data;
            },
            async (error: AxiosError) => {
                const {response} = error;
                return Promise.reject(error);
            },
        );
    }

}

let httpAxios = new HttpAxios(config).service;
export default httpAxios